/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.dao.GatherBillItemDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class GatherBillItemDaoImpl extends BaseDaoImpl
		implements
			GatherBillItemDao {
	public List getListByGatherBill(long billId) {
		String sql = "from com.minierp.model.GatherBillItem s where s.billId="
				+ billId + " order by s.id";
		return this.getHibernateTemplate().find(sql);
	}

	public List getListByCooperBill(long billId) {
		String sql = "select b,s,cbItem from com.minierp.model.GatherBill b ,com.minierp.model.GatherBillItem s ,com.minierp.model.CooperBillItem cbItem where b.id=s.billId and s.cbItemId=cbItem.id and b.enabled=\'Y\' and b.cooperBillId="
				+ billId + " order by s.id";
		return this.getHibernateTemplate().find(sql);
	}

	public int delete(final long id) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.GatherBillItem where id=:id";
						return new Integer(session.createQuery(hql)
								.setLong("id", id).executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public int deleteByGatherBillId(final long billId) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.GatherBillItem where billId=:billId";
						return new Integer(session.createQuery(hql)
								.setLong("billId", billId).executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public BigDecimal getGatheredAmount(long bbItemId) {
		String sql = "select sum(i.amount) from com.minierp.model.GatherBill b ,com.minierp.model.GatherBillItem i where b.id=i.billId and i.cbItemId="
				+ bbItemId + " and b.enabled=\'Y\'";
		List list = this.getHibernateTemplate().find(sql);
		return list.size() > 0 && list.get(0) != null ? (BigDecimal) list
				.get(0) : new BigDecimal(0);
	}
}